﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.Web.UserControls;
using NPMonitor.Implementation;
using NPMonitor.DataModel;
using System.Collections.ObjectModel;

namespace NPMonitor.Web
{
    public partial class Locations : BasePage
    {
        #region 属性
        /// <summary>
        /// 页面大小
        /// </summary>
        public int PageSize
        {
            get { return ctPager.PageSize; }
        }

        /// <summary>
        /// 当前页数
        /// </summary>
        public int CurrentPageIndex
        {
            get { return ctPager.CurrentPageIndex; }
        }

        /// <summary>
        /// 获取PagerControl
        /// </summary>
        public PagerControl ListPagerControl { get { return this.ctPager; } }


        public int PoolCount
        {
            get
            {
                return Convert.ToInt32(ViewState["PoolCount"]);
            }
            set
            {
                ViewState["PoolCount"] = value;
            }
        }

        public bool PhEdit
        {
            get
            {
                return phPoolInfo.Visible;
            }
            set
            {
                phPoolInfo.Visible = value;
            }
        }

        #endregion
        #region 方法
        public override string FunctionName
        {
            get { return FunctionEnum.Locations.ToString(); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ctPager.PageChange += new PagerControl.PageRefresh(PoolDataBind);
            if (!IsPostBack)
            {
                PoolDataBind(0);
            }
        }

        public void PoolDataBind(int pageIndex)
        {
            if (pageIndex == -1)
            {
                pageIndex = ctPager.CurrentPageIndex;
            }
            PoolCount = StoreManager.Current.MachineStore.GetMachinePoolCount();
            ctPager.IntialProperties(PoolCount);
            List<MachinePool> list = new List<MachinePool>();
            if (pageIndex > ctPager.TotalPage - 1)
            {
                pageIndex = ctPager.TotalPage - 1;
            }
            list = StoreManager.Current.MachineStore.GetAllMachinePool(pageIndex, PageSize);
            rpPoolList.DataSource = list;
            rpPoolList.DataBind();
        }

        protected void imgRefresh_OnClick(object sender, EventArgs e)
        {
            PoolDataBind(ctPager.CurrentPageIndex);
        }

        protected void lbtnCreatePool_Click(object sender, EventArgs e)
        {
            cleanPoolControl();
            PhEdit = true;
        }

        protected void cleanPoolControl()
        {
            ctPoolInfo.PoolId = 0;
            ctPoolInfo.PoolName = "";
            ctPoolInfo.Comments = "";
        }

        protected void rpPoolList_OnItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                int poolId = Convert.ToInt32(e.CommandArgument);
                if (e.CommandName.Trim() == "DeletePool")
                {
                    StoreManager.Current.MachineStore.DeletePoolById(poolId);
                }

                if (e.CommandName.Trim() == "EditPool")
                {
                    var pool = StoreManager.Current.MachineStore.GetMachinePoolInfoById(poolId);
                    if (pool != null)
                    {
                        ctPoolInfo.PoolId = poolId;
                        ctPoolInfo.PoolName = pool.PoolName;
                        ctPoolInfo.Comments = pool.Comments;
                    }
                    PhEdit = true;
                }
                PoolDataBind(ctPager.CurrentPageIndex);
            }
        }
        protected void rpPoolList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            MachinePool pool = e.Item.DataItem as MachinePool;
            if (pool != null)
            {
                LinkButton lbtnDeletePool = e.Item.FindControl("lbtnDelete") as LinkButton;
                lbtnDeletePool.OnClientClick = string.Format("return DeletePoolValidate({0});", pool.PoolId);
            }
        }
    }
        #endregion
}